﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proxys.Checkers
{
    public class PropertiesProxyChecker : PropertiesReader
    {
        private List<ProxyChecker> proxyCheckers;

        public List<ProxyChecker> ProxyCheckers
        {

            get { return proxyCheckers; }
        }

        public static PropertiesProxyChecker ReadProperties(string filePath = "checker.properties") 
        {
            return new PropertiesProxyChecker(filePath);
        }


        public PropertiesProxyChecker(string filePath)
            : base(filePath)
        {
            proxyCheckers = new List<ProxyChecker>();
            string[] checkerNames = getNames("names");
            foreach (string checkerName in checkerNames) 
            {
                proxyCheckers.Add(getChecker(checkerName));
            }
        }

        private ProxyChecker getChecker(string checkerName)
        {
            string testUrl = getPropertyValue(checkerName + ".testUrl","http://www.baidu.com");
            string key = getPropertyValue(checkerName + ".key","baidu");
            string fileName = getPropertyValue(checkerName + ".fileName","checkec_proxy.txt");
            string maxDelayTime = getPropertyValue(checkerName + ".maxDelayTime","2000");
            string encode = getPropertyValue(checkerName + ".encode", "GBK");
            return new ProxyChecker()
            {
                TestUrl = testUrl,
                FileName = fileName,
                MaxDelayTime = Int32.Parse(maxDelayTime),
                Key = key,
                Encode = encode
            };
        }
    }
}
